/*
  Copyright © 2010  Vinícius dos Santos Oliveira

      This file is part of Touché.

      Touché is free software: you can redistribute it and/or modify
      it under the terms of the GNU Lesser General Public License as published
      by the Free Software Foundation, either version 3 of the License, or
      (at your option) any later version.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU Lesser General Public License for more details.

      You should have received a copy of the GNU Lesser General Public License
      along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */

#ifndef STAGEACTOR_H
#define STAGEACTOR_H

#include "toucheelement.h"
#include "widgets/draggabletogglebutton.h"
#include "widgets/pushtextbuttonitem.h"
#include <QGraphicsView>

#define TOUCHE_SHOULD_SKIP_THIS

#include "elements/textelement.h"
#include "elements/scribbleelement.h"
#include "elements/audioelement.h"
#include "elements/photoelement.h"

#undef TOUCHE_SHOULD_SKIP_THIS

namespace Touche
{
    class StageActorLink;
    class ColaborativeController;

    class StageActor: public QObject
    {
        friend class Stage;
        friend class StageActorLink;
        friend class ColaborativeController;
        Q_OBJECT

        /*
          variable that contains the adress of the StageActor object with menu opened
          */
        static StageActor *opened;
        static QHash<QUuid, Element *> m_elements;
        static QHash<Element *, StageActor *> m_actorsByElement;
        static QList<StageActor *> m_actors;

        // pointer to where to link
        static StageActor *linkPower;

        Element *m_element;
        bool locked;
        DraggableToggleButton *m_item;
        QGraphicsView *view;

        QVector<QString> menuOptions;
        QVector<PushTextButtonItem *> menuButtons;
        PushTextButtonItem *defaultMenuButtons[2];

        static QGraphicsScene *dialog;
        QList<StageActorLink *> links;

        QPointF m_lastPos;
        QPointF newPos;

    public:
        StageActor(Element *element);
        ~StageActor();

        Element *element();
        QGraphicsObject *item() const;

        void calculateForces();
        bool advance();

        void setUuid(const QUuid &);

        static const QHash<QUuid, Element *> &elements();
        static StageActor *getActor(Element *element);
        static const QList<StageActor *> &actors();

        static Element *makeElement(const QString &type);

    private slots:
        //esses slots deveriam receber a lista de opções que o menu possui
        void onButtonToggled(bool);
        void onDoubleClicked();
        void onLink();
        void openMenu();
        void closeMenu();
        void onMenuActived(PushButtonItem *button);
        void onMenuUpdated(const QVector<QString> &newMenu);
        void onElementLocked(bool locked); // TODO
        void onContentsChanged();
        void onSceneRequest();
        void onStageRequest();
        void onDragged(DraggableToggleButton *sink);
    };
}

inline Touche::Element * Touche::StageActor::element()
{
    return m_element;
}

inline QGraphicsObject * Touche::StageActor::item() const
{
    return m_item;
}

inline Touche::Element *Touche::StageActor::makeElement(const QString &type)
{
    if(type == "audio"){
        return new AudioElement;
    }else if(type == "photo"){
        return new PhotoElement;
    }else if(type == "scribble"){
        return new ScribbleElement;
    }else if(type == "text"){
        return new TextElement;
    }else{
        return NULL;
    }
}

#endif // STAGEACTOR_H
